// baidumap://map/marker 可以成功的地址
import { userStore } from '@/stores/user.js'
import { ref } from "vue"


export const useActionSheet = () => {
  const data = null
  const show = ref(false)
  const actions = [
    { name: '高德地图' },
    { name: '百度地图' }
  ]
  const navigationClick = () => {
    show.value = true
  }
  const onSelect = (item, data, isReversal) => {
    show.value = false
    if (isReversal) {
      const lon = data.latitude
      const lat = data.longitude
      data.latitude = lat
      data.longitude = lon
    }
    navigation(item.name, data)
  }
  return {show, actions, navigationClick, onSelect }
}

export const navigation = (mapName, destinationInfo) => {
  const { address, latitude, longitude, scenicAreaName } = destinationInfo
  console.log('address, latitude, longitude, scenicAreaName',address, latitude, longitude, scenicAreaName)
  const user = userStore()
  const myLongitude = user.position?.coords?.longitude
  const myLatitude = user.position?.coords?.latitude

  console.log('经度' , user.position?.coords?.longitude)
  console.log('维度' , user.position?.coords?.latitude)

  const isWeChat = /MicroMessenger/i.test(window.navigator.userAgent)
  let url
  if (mapName === '百度地图') {
    if (isWeChat) {
      let origin = ''
      if (myLatitude && myLongitude) {
        origin = `latlng:${myLatitude},${myLongitude}|name:我的位置`
        let destination = ''
        if (latitude && longitude) {
          destination = `latlng:${longitude},${latitude}|name:${scenicAreaName}`
        } else {
          destination = `${scenicAreaName}`
        }
        url = `http://api.map.baidu.com/direction?origin=${origin}&destination=${destination}&mode=driving&region=成都&output=html&src=webapp.baidu.openAPIdemo`
      } else {
        if (latitude && longitude) {
          url = `http://api.map.baidu.com/marker?location=${longitude},${latitude}&title=${address}&content=${address}&output=html&src=webapp.baidu.openAPIdemo`
        } else {
          url = `http://api.map.baidu.com/geocoder?address=${address}&output=html&src=webapp.baidu.openAPIdemo`
        }
      }
    } else {
      if (myLatitude && myLongitude) {
        let origin = `latlng:${myLatitude},${myLongitude}|name:我的位置`
        let destination = ''
        if (latitude && longitude) {
          destination = `latlng:${longitude},${latitude}|name:${scenicAreaName}`
        } else {
          destination = `${scenicAreaName}`
        }
        url = `baidumap://map/direction?origin=${origin}&destination=${destination}&coord_type=bd09ll&mode=transit&sy=3&index=0&target=1&src=andr.baidu.openAPIdemo`
      } else if (latitude && longitude) {
        url = `baidumap://map/marker?location=${longitude},${latitude}&title=${scenicAreaName}&content=${address}&traffic=on&src=andr.baidu.openAPIdemo`
      } else {
        url = `bdapp://map/geocoder?src=andr.baidu.openAPIdemo&address=${address}`
      }
    }
  } else if (mapName === '高德地图') {
    if (isWeChat) {
      if (latitude && longitude) {
        url = `https://uri.amap.com/navigation?to=${latitude},${longitude},${scenicAreaName}&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0`
      } else {
        url = `https://uri.amap.com/search?keyword=${scenicAreaName}&center=121.515601,31.233456&city=310000&view=map&src=mypage&coordinate=gaode&callnative=0`
      }
    } else {
      if (latitude && longitude) {
        url = `amapuri://route/plan/?sid=&did=&dlat=${longitude}&dlon=${latitude}&dname=${scenicAreaName}&dev=0&t=0`
      } else {
        url = `androidamap://keywordNavi?sourceApplication=softname&keyword=${scenicAreaName}&style=2`
      }
    }
  }
  const idA = 'naviA'
  document.getElementById(idA)?.remove();
  const link = document.createElement("a")
  link.setAttribute('id',idA)
  link.href = url
  link.textContent = ""
  document.body.appendChild(link)
  link.click()
}
